package com.huang.edustatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.edustatistics.client.UcenterClient;
import com.huang.edustatistics.entity.StatisticsDaily;
import com.huang.edustatistics.mapper.StatisticsDailyMapper;
import com.huang.edustatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hunag.utils.R;
import com.netflix.loadbalancer.RandomRule;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-04
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {


    @Autowired
    private UcenterClient ucenterClient;

    //生成统计数据
    @Override
    public void getRegisterNum(String day) {
        //删除查过的  重新加入数据库
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        R registerNum = ucenterClient.getRegisterNum(day);
        Integer countRegister =(Integer) registerNum.getData().get("count");
        StatisticsDaily daily = new StatisticsDaily();
        daily.setDateCalculated(day);
        daily.setRegisterNum(countRegister);
        daily.setLoginNum(RandomUtils.nextInt(100,200));
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));
        daily.setCourseNum(RandomUtils.nextInt(100,200));
        daily.setGmtCreate(new Date());
        daily.setGmtModified(new Date());

        baseMapper.insert(daily);

    }

    //图标显示
    @Override
    public Map<String,Object> showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for (StatisticsDaily daily : staList) {
            date_calculatedList.add(daily.getDateCalculated());

            switch (type){
                case "login_num" : numList.add(daily.getLoginNum());
                    break;
                case "register_num" : numList.add(daily.getRegisterNum());
                    break;
                case "video_view_num" : numList.add(daily.getVideoViewNum());
                    break;
                case "course_num" : numList.add(daily.getCourseNum());
                    break;
            }


        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("date",date_calculatedList);
        map.put("num",numList);

        return map;
    }
}
